<template>
  <div class="login-wrap"  @keyup.enter = "submit" >

    <el-form class="login-form" label-position="top" label-width="80px" :model="loginForm">
      <h2>用户登录</h2>
      <el-form-item label="账号">
        <el-input ref ='focus' v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-button class ="login-btn" @click.prevent="submit" type="primary">提交</el-button>
    </el-form>

  </div>
</template>

<script>
export default {
  name: 'Login',
  mounted () {
    this.$refs.focus.focus()
  },
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    async submit () {
      const res = await this.$http.post('login', this.loginForm)
      const {data, meta: {msg, status}} = res.data
      if (status === 200) {
        localStorage.setItem('token', data.token)
        this.$router.push({
          name: 'home'
        })
        this.$message.success(msg)
      } else {
        this.$message.warning(msg)
      }
    }
  }
}
</script>

<style>
  .login-wrap{
    height: 100%;
    background-color: #324152;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .login-wrap .login-form{
    width: 400px;
    background-color: #fff;
    border-radius: 5px;
    padding: 5px

  }
  .login-wrap .login-btn{
    width: 100%
  }
</style>
